import Vue from 'vue'

//注册组件
const registerComponent = function(oMenu) {
    let oComponent = () => import('@/views/' + oMenu.router);
    if (!oComponent) {
        return;
    }
    Vue.component(oMenu.code, oComponent);
};

//注册路由
const fRegisterRouter = function(aComponents) {
    let oComponents = {};
    aComponents.map((oComp) => {
        oComponents[oComp.name.substring(4).toLowerCase()] = oComp
    })
    return function(router, oMenu, parentName) {
        const sRouteName = oMenu.code;
        let oComponent;
        switch (oMenu.menuType) {
            case 'frame':
                oComponent = oComponents[oMenu.router];
                break;
            case 'project':
                oComponent = () => import('@/views/' + oMenu.router);
                break;
        }
        if (!oComponent) {
            return;
        }
        if (parentName) {
            // 有父级路由
            router.addRoute(parentName, {
                name: sRouteName,
                path: sRouteName,
                component: oComponent,
                props: oMenu.props
            });
        } else {
            // 没有父级路由
            router.addRoute({
                name: sRouteName,
                path: '/' + sRouteName,
                component: oComponent,
                props: oMenu.props
            });
        }
    }
};

export { registerComponent, fRegisterRouter };